package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.NoticeService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.NoticeMapper;
import cn.icare.cms.dao.po.MoneyOut;
import cn.icare.cms.dao.po.Notice;
import cn.icare.cms.model.ro.AddAreaRO;
import cn.icare.cms.model.ro.AddNoticeRO;
import cn.icare.cms.model.ro.EditAreaRO;
import cn.icare.cms.model.ro.EditNoticeRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    private final Logger logger = LoggerFactory.getLogger(NoticeServiceImpl.class);

    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public Integer createOne(AddNoticeRO ro, String actionUserName) {
        try {
            Notice notice = new Notice();
            notice.setKind(ro.getKind());
            notice.setTitle(ro.getTitle());
            notice.setContent(ro.getContent());
            notice.setCreatetime(new Date());
            notice.setCreator(actionUserName);
            int result = this.noticeMapper.insertSelective(notice);
            if (result > 0) {
                return notice.getId();
            }
        } catch (Exception ex) {
            logger.error("createOne 异常", ex);
        }
        return 0;
    }

    @Override
    public boolean updateOne(EditNoticeRO ro, String actionUserName) {
        boolean isSuccess = false;
        Notice notice = new Notice();
        notice.setId(ro.getId());
        notice.setTitle(ro.getTitle());
        notice.setContent(ro.getContent());
        notice.setKind(ro.getKind());
        notice.setCreatetime(new Date());
        notice.setCreator(actionUserName);
        if (this.noticeMapper.updateByPrimaryKeySelective(notice) > 0) {

            isSuccess = true;
        }
        return isSuccess;
    }

    @Override
    public boolean deleteOne(Integer id) {
        return this.noticeMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Notice.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.noticeMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public Notice getOne(Integer id) {
        return this.noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(Integer kind, String title, Integer pageNum, Integer pageSize) {
        MsgDatasVO result = new MsgDatasVO();
        try {

            PageHelper.startPage(pageNum, pageSize);
            Example example = new Example(Notice.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty(title)) {
                criteria.andLike("title", "%" + title + "%");
            }
            if (kind > 0) {
                //流水号
                criteria.andEqualTo("kind", kind);
            }

            List<Notice> dataList = this.noticeMapper.selectByExample(example);
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());
            result.setDatas(dataList);

        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }

    @Override
    public boolean exists(Integer id, String title) {
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("title", title);
        criteria.andNotEqualTo("id", id);
        int result = this.noticeMapper.selectCountByExample(example);
        return result > 0;
    }
}
